#coding=utf-8

import urllib2   
import datetime  
import string
import csv  
  
separate_line = '-----------separate_line-----------'  
  
def copy1(fromfile, tofile):   
    f = file(fromfile,"rU")
    f2 = file(tofile,"wb")
    
    while True:        
        record = ""
        a = f.readline()
        if a == "":
            break
        if a.find(u"第") >= 0:
            datestr = f.readline()
            
            record += datestr[0:10] + "\r\n"
            record += "created_time:" + string.rstrip(datestr, '\r\n') + "\r\n"
            record += string.rstrip(a, '\r\n') + u"！" + "\r\n\r\n"
            
        else:
            date = datetime.datetime.strptime(string.rstrip(a, '\r\n'), "%Y.%m.%d")
            record += date.strftime("%Y-%m-%d") + "\r\n"
            datestr = f.readline()
            record += "created_time:" + string.rstrip(datestr, '\r\n') + "\r\n"
        
        #content
        while True:
                c = f.readline()
                if c == "":
                    break                
                record += string.rstrip(c, '\r\n') + "\r\n"
                if c.find(separate_line) == 0:
                    break 
        f2.write(record)
        
        #process lines
              
    
    f.close
    #print record
    f2.close
    


separate_lines = """






"""


def copy2(fromfile, tofile):   
    f = file(fromfile,"rU")
    f2 = file(tofile,"wb")
    print separate_lines
    while True:
        record = ""
        title = f.readline()
        if title == "":
            break
        
        titles = title.split(" ")
        b3 = False
        if len(titles) == 3:
            dd = titles[0] + string.rstrip(titles[2], '\r\n')
            b3 = True
        else:
            dd = titles[0]+titles[2]
        datet = datetime.datetime.strptime(dd, u"%Y年%m月%d日%H时%M分")
        record += datet.strftime("%Y-%m-%d") + "\r\n"
        record += "created_time:" + datet.strftime("%Y-%m-%d %H:%M") + "\r\n"
        if len(titles) > 3:
            record += u"今日天气：" + string.rstrip(titles[5], '\r\n') + u"。" + "\r\n"
        
        
        #content
        while True:
                c = f.readline()
                if c == "":
                    break
                if not (b3 == True and c == "\n"):
                    record += string.rstrip(c, '\r\n') + "\r\n"
                b3 = False
                if c.find(separate_line) == 0:
                    break                
        f2.write(record)
    
    f2.close()
    f.close() 
        
       
       
def csv2txt(fromfile, tofile):   
    f = file(fromfile,"r")
    f2 = file(tofile,"wb")
    
    
    reader = csv.reader(open(fromfile), "excel")
    
    reader.next()
    
 
    i = 0
    for row in reader:
        i += 1
        f2.write(row[0])
        
        f2.write("\r\n")
        
        f2.write(row[1])
        
        
        if row[2] != "":
            f2.write("\r\n")
            f2.write(u"\r\n心得杂感：\r\n")            
            f2.write(row[2])
            
        
        if row[3] != "":
            f2.write("\r\n")
            f2.write(u"\r\n明日计划：\r\n")
            f2.write(row[3])
        
        f2.write("\r\n" + separate_line + str(i) + "\r\n")

    
    f2.close()
    f.close()        
if __name__ == '__main__':   
       
    #copy1(u"d:/个人信息/diary/diary.txt",u"d:/个人信息/diary/diary2.txt")
    
    #copy2(u"d:/个人信息/diary/日记.txt",u"d:/个人信息/diary/日记2.txt")    
    #copy2(u"d:/个人信息/diary/liyong的日记.txt",u"d:/个人信息/diary/liyong的日记2.txt")
    csv2txt(u"d:/个人信息/diary/2002-4.csv", u"d:/个人信息/diary/2002-4.txt")